package com.kefu.web.im.config.web.handle;

import java.net.InetSocketAddress;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.kefu.web.im.config.IMDataContext;
import com.kefu.web.im.config.web.message.ChatMessage;
import com.kefu.web.im.config.web.message.NewRequestMessage;
import com.kefu.web.im.config.web.message.SeatStatusMessage;
import com.kefu.web.im.domain.MessageOutContent;
import com.kefu.web.im.domain.SeatUser;
import com.kefu.web.im.netty.client.NettyClients;
import com.kefu.web.im.service.quene.ServiceQuene;
import com.kefu.web.im.util.DateUtils;
import com.kefu.web.im.util.MessageUtils;

public class IMEventHandler {
	protected SocketIOServer server;

	@Autowired
	public IMEventHandler(SocketIOServer server) {
		this.server = server;
	}

	@OnConnect
	public void onConnect(SocketIOClient client) {
		try {
			String user = client.getHandshakeData().getSingleUrlParam("userid");
			String orgi = client.getHandshakeData().getSingleUrlParam("orgi");
			String session = client.getHandshakeData().getSingleUrlParam("session");
			String appid = client.getHandshakeData().getSingleUrlParam("appid");
			String agent = client.getHandshakeData().getSingleUrlParam("agent");
			String skill = client.getHandshakeData().getSingleUrlParam("skill");

			if (!StringUtils.isBlank(user)) {
				/**
				 * 用户进入到对话连接 ， 排队用户请求 , 如果返回失败，表示当前坐席全忙，用户进入排队状态，当前提示信息 显示
				 * 当前排队的队列位置，不可进行对话，用户发送的消息作为留言处理
				 */
				InetSocketAddress address = (InetSocketAddress) client.getRemoteAddress();
				//TODO
				NewRequestMessage newRequestMessage = new NewRequestMessage();
//				/**
//				 * 加入到 缓存列表
//				 */
				NettyClients.getInstance().putIMEventClient(user, client);
//				
				if (newRequestMessage != null && !StringUtils.isBlank(newRequestMessage.getMessage())) {
					MessageOutContent outMessage = new MessageOutContent();
					outMessage.setMessage(newRequestMessage.getMessage());
					outMessage.setMessageType(IMDataContext.MessageTypeEnum.MESSAGE.toString());
					outMessage.setCalltype(IMDataContext.CallTypeEnum.IN.toString());
					outMessage.setNickName(newRequestMessage.getUsername());
					outMessage.setCreatetime(DateUtils.formatDate(new Date()));

					client.sendEvent(IMDataContext.MessageTypeEnum.STATUS.toString(), outMessage);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 添加@OnDisconnect事件，客户端断开连接时调用，刷新客户端信息
	@OnDisconnect
	public void onDisconnect(SocketIOClient client) {
		String user = client.getHandshakeData().getSingleUrlParam("userid");
		String orgi = client.getHandshakeData().getSingleUrlParam("orgi");
		if (user != null) {
			try {
				/**
				 * 用户主动断开服务
				 */
				// TODO
//				SeatUser seatuser = (SeatUser) CacheHelper.getAgentUserCacheBean().getCacheObject(user, IMDataContext.SYSTEM_ORGI);
				SeatUser seatuser = null;
				ServiceQuene.serviceFinish(seatuser, orgi);
			} catch (Exception e) {
				e.printStackTrace();
			}
			NettyClients.getInstance().removeIMEventClient(user, client.getSessionId().toString());
		}
	}

	// 消息接收入口，网站有新用户接入对话
	@OnEvent(value = "new")
	public void onEvent(SocketIOClient client, AckRequest request, NewRequestMessage data) {

	}

	// 消息接收入口，坐席状态更新
	@OnEvent(value = "agentstatus")
	public void onEvent(SocketIOClient client, AckRequest request, SeatStatusMessage data) {
		System.out.println(data.getMessage());
	}

	// 消息接收入口，收发消息，用户向坐席发送消息和 坐席向用户发送消息
	@OnEvent(value = "message")
	public void onEvent(SocketIOClient client, AckRequest request, ChatMessage data) {
		if (data.getType() == null) {
			data.setType("message");
		}
		if (!StringUtils.isBlank(data.getMessage()) && data.getMessage().length() > 300) {
			data.setMessage(data.getMessage().substring(0, 300));
		}
		MessageUtils.createMessage(data, IMDataContext.MediaTypeEnum.TEXT.toString(), data.getUserid());
	}
}